package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.pojo.Courses;
import com.entity.pojo.CoursesRelation;
import com.mapper.CoursesRelationMapper;
import com.service.CoursesRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.CoursesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程与课程之间的后继关系 服务实现类
 * </p>
 *
 * @author chenMo
 * @since 2022-04-06
 */

@Service
public class CoursesRelationServiceImpl extends ServiceImpl<CoursesRelationMapper, CoursesRelation> implements CoursesRelationService {

    @Resource
    private CoursesService coursesService;

    @Override
    public List<Courses> getCanBeAddedNextCourseList(Integer currentCourseId) {
        // 先查该课程已有的后继课程id
        List<Integer> courseIds = baseMapper.selectList(new QueryWrapper<CoursesRelation>()
                .eq("courseId", currentCourseId))
                .stream()
                .map(CoursesRelation::getNextCourseId)
                .collect(Collectors.toList());
        // 再将自身的课程id加进去
        courseIds.add(currentCourseId);
        return coursesService.list(new QueryWrapper<Courses>()
                .notIn("courseId", courseIds)
                .select("courseId", "courseName"));
    }

    @Override
    public boolean addCourseRelation(CoursesRelation coursesRelation) {
        baseMapper.insert(coursesRelation);
        return updateNextCourseStr(coursesRelation.getCourseId());

    }

    @Transactional
    @Override
    public boolean deleteCourseRelation(Integer courseId, Integer nextCourseId) {
        baseMapper.delete(new QueryWrapper<CoursesRelation>()
        .eq("courseId",courseId)
        .eq("nextCourseId",nextCourseId));
        return updateNextCourseStr(courseId);
    }

    // 根据课程id更新课程表中的后继课程字符串
    private boolean updateNextCourseStr(Integer courseId) {
        // 查出该课程的所有后继课程id
        List<Integer> nextCourseIdList = baseMapper.selectList(new QueryWrapper<CoursesRelation>()
                .eq("courseId", courseId)
                .select("nextCourseId"))
                .stream().map(CoursesRelation::getNextCourseId)
                .collect(Collectors.toList());
        if (nextCourseIdList.isEmpty()) {
            // 如果查询结果为空，则直接置空关联课程字符串
//            Courses course = coursesService.getById(courseId).setNextCourseStr("");
            Courses course = coursesService.getById(courseId);
            course.setNextCourseStr("");
            return coursesService.updateById(course);
        }
        // 如果查询结果不为空，则查询该课程的所有关联课程名称
        List<String> nextCourseNameList = coursesService.listByIds(nextCourseIdList)
                .stream().map(Courses::getCourseName)
                .collect(Collectors.toList());
        // 拼接成字符串，以冒号分割
        String nextCourseStr = String.join(":", nextCourseNameList);
        // 更新该课程在课程表中的关联课程字符串
        Courses course = coursesService.getById(courseId);
        course.setNextCourseStr(nextCourseStr);
        return coursesService.updateById(course);
    }

}
